#ifndef __RASTERSCAN_H__
#define __RASTERSCAN_H__


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* #include <malloc.h> */
#include <string.h>
#include <sys/types.h>
#ifndef __COMPLEX_H__
#define __COMPLEX_H__
#include <complex.h>
#endif

/* ------------------------ Type Definitions ------------------------ */

typedef struct RS_PNMIMAGE 
{
  unsigned char **rawImage;
  int nrows, ncols;
  char name[72];
} RS_IMG, *RS_IMGPTR;

/* TODO: move this to the API */
typedef enum eRasterScanDisplayType{
  RASTERSCAN_NO_DISPLAY,
  RASTERSCAN_DISPLAY_END,
  RASTERSCAN_DISPLAY_RASTERSCAN_ANIMATION
}RASTERSCANDISPLAYTYPE;

#define MAXINTERVAL                       100

/* ------------------------ Private Attributes ------------------------ */

unsigned int loopCountX;               // counter for the x direction
unsigned int loopCountY;               // counter for the y direction
unsigned int printAnimation;
unsigned int pixelFixCount;
unsigned char currentLabel;
unsigned char buf[64];                 // buffer for line read from file 
RS_IMG rsImage;                      // raw image data

/* ------------------------ Function Mappings ------------------------ */

#define p_initRasterScan                  i_initRasterScan
#define p_printRasterScanMatrix           i_printRasterScanMatrix
#define p_startAutoRasterScan             i_startAutoRasterScan
#define p_deallocRasterScan               i_deallocRasterScan

/* ------------------------ Inlined Methods ------------------------ */

#define p_rasterScanGetNumberOfPixels()      (rsImage.nrows * rsImage.ncols)
#define CurrentPixelValue                    rsImage.rawImage[loopCountX][loopCountY]
#define WestPixelValue                       rsImage.rawImage[loopCountX-1][loopCountY]
#define NorthPixelValue                      rsImage.rawImage[loopCountX][loopCountY-1]
#define isWestPositive()                     ('0' != WestPixelValue)
#define isNorthPositive()                    ('0' != NorthPixelValue)
#define isWestLabeled()                      (('0' != WestPixelValue) & \
                                              ('1' != WestPixelValue))
#define isNorthLabeled()                     (('0' != NorthPixelValue) & \
                                              ('1' != NorthPixelValue))

/* ------------------------ Public Methods ------------------------ */

void i_initRasterScan(char * rasterScanFileName);
void i_printRasterScanMatrix(void);
void i_startAutoRasterScan(RASTERSCANDISPLAYTYPE rasterScanDisplayType);
void i_deallocRasterScan(void);

/* ------------------------ Private Methods ------------------------ */

void rasterScan(void);
unsigned char **rasterScanAllocateMatrix(int length, int height);
unsigned int rasterScanCheckOne(void);
unsigned int rasterScanCheckTwo(void);
unsigned int rasterScanCheckThree(void);
unsigned int rasterScanCheckFour(void);
unsigned int rasterScanCheckFive(void);
void setCurrentEqualToWest(void);
unsigned int westCrossCheck(void);
unsigned int northCrossCheck(void);

#endif
